package com.winkyle.woodenfish.controller.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.adapter.MainContentFragmentPagerAdapter;
import com.winkyle.woodenfish.controller.fragment.ItemsFragment;
import com.winkyle.woodenfish.controller.fragment.MessageFragment;
import com.winkyle.woodenfish.controller.fragment.AccountDataFragment;
import com.winkyle.woodenfish.controller.fragment.TeamFragment;
import com.winkyle.woodenfish.mvpview.MainView;
import com.winkyle.woodenfish.databinding.ActivityMainBinding;
import com.winkyle.woodenfish.factory.presenterfactory.CreatePresenter;
import com.winkyle.woodenfish.presenter.MainPresenter;
import com.winkyle.woodenfish.widget.BottomBar;
import com.winkyle.woodenfish.widget.BottomBarTab;
import com.winkyle.woodenfish.widget.CustomDialog;
import com.winkyle.woodenfish.widget.NoPreLoadViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author winkyle
 * @date 2018/06/13
 * @description 继承自 {@link AbstractMvpActivity}的活动类MainActivity，app的主页
 *
 * https://www.cnblogs.com/hwb04160011/p/7027317.html  写界面
 */
@CreatePresenter(value = MainPresenter.class)
public class MainActivity extends AbstractMvpActivity<MainView,MainPresenter> implements MainView{

    private ActivityMainBinding activityMainBinding;
    /**
     * @description 活动创建，指定layout布局
     * @param savedInstanceState 保存的活动状态的数据
     * @return
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        initActivity();
    }

    private BottomBarTab message;
    private MainContentFragmentPagerAdapter mainContentFragmentPagerAdapter;
    /**
     * @description 初始化活动的一些数据和动作
     * @return
     */
    public void initActivity(){
        activityMainBinding.setMainView(this);
        List<Fragment> fragments = new ArrayList<>();
        //初始化四个碎片
        ItemsFragment itemsFragment = new ItemsFragment();
        TeamFragment teamFragment = new TeamFragment();
        MessageFragment messageFragment = new MessageFragment();
        AccountDataFragment personalDataFragment = new AccountDataFragment();
        //将四个碎片加进List
        fragments.add(itemsFragment);
        fragments.add(teamFragment);
        fragments.add(messageFragment);
        fragments.add(personalDataFragment);
        mainContentFragmentPagerAdapter = new MainContentFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        //将List传入适配器
        activityMainBinding.contentContainer.setAdapter(mainContentFragmentPagerAdapter);
        activityMainBinding.contentContainer.addOnPageChangeListener(new NoPreLoadViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                activityMainBinding.bottomBar.setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        message = new BottomBarTab(this, R.drawable.icon_message, "消息");
        activityMainBinding.bottomBar.addItem(new BottomBarTab(this, R.drawable.icon_items, "项目"))
                .addItem(new BottomBarTab(this, R.drawable.icon_team, "团队"))
                .addItem(message)
                .addItem(new BottomBarTab(this, R.drawable.icon_me, "我的"));
        activityMainBinding.bottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                activityMainBinding.contentContainer.setCurrentItem(position,false);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }
    /**
     * @description  点击返回，弹出是否退出软件提示
     * @param keyCode 按键码
     * @param event 事件
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new CustomDialog(this)
                    .setTitleText("提示")
                    .setContentText("确定退出木鱼么？")
                    .setConfirmText("确定")
                    .setCancelText("取消")
                    .setConfirmListener(new CustomDialog.DialogClickListener() {
                        @Override
                        public void onClick(CustomDialog customDialog) {
                            finish();
                        }
                    })
                    .show();
        }
        return false;
    }

}
